/**
 * @Title: ResolveDomain.java
 * @Package io.renren.modules.iots.utils.comment
 * @Description: TODO(用一句话描述该文件做什么)
 * @author 周西栋
 * @date 2018年6月19日
 * @version V1.0
 */
package io.renren.modules.iots.utils.comment;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.apache.log4j.Logger;


/**
 * @ClassName: ResolveDomain
 * @Description: 动态解析IP地址
 * @author 周西栋
 * @date 2018年6月19日
 *
 */
public class ResolveDomain {
	
	private Logger log = Logger.getLogger(ResolveDomain.class);
    
    public static void main(String[] args) throws UnknownHostException {  
        ResolveDomain rd = new ResolveDomain();
        rd.resolveDomain("www.baidu.com", "114.114.114.114", new ConcurrentLinkedQueue<String>());
    }  

    /**
     * @author 周西栋
     * @date 2018年6月19日
     * @Title: resolveDomain
     * @Description: 根据DNS解析域名domain
     * @param @param domain
     * @param @param DNS
     * @param @param queue
     * @param @return    参数
     * @return List<InetAddress>    返回类型
     * @throws
     */
    public List<InetAddress> resolveDomain(String domain, String DNS, ConcurrentLinkedQueue<String> queue){
        System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun");
        System.setProperty("sun.net.spi.nameservice.nameservers", DNS);        
        InetAddress[] addresses;
        try {
            addresses = InetAddress.getAllByName(domain);    //IP or domain
            for (int i = 0; i < addresses.length; i++) {
                String ip = addresses[i].getHostAddress();
                log.info(DNS + "\t" + domain + "\t" + ip);
                queue.add(DNS + "\t" + domain + "\t" + ip);
            } 
            if(addresses != null && addresses.length > 0){
            	List<InetAddress> list = new ArrayList<>();
            	for(int i = 0; i < addresses.length; i++){
            		list.add(addresses[i]);
            	}
            	return list;
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

}
